import { reactive, toRefs } from 'vue';
import IWebsocket from '../chatWS'
import { Chat } from '@/store/chat';
import pinia from '@/store/index';
const chat = Chat(pinia);
const state_data: any = reactive({
  Input: null,
  page: 1,
});

const usechatHooks = (state?: any) => {
  // 解析消息体
  const decodeContent = (data: any, name: string) => {
    let MessageOutputeItem = state.root.lookupType(name)
    const buffer1 = new Uint8Array(data);
    const decodedMessage2 = MessageOutputeItem.decode(buffer1);

    return MessageOutputeItem.toObject(decodedMessage2);
  }

  const encodeInput = (type: any, request_id: any, data: any) => {
    state_data.Input = state.root.lookupType('Input');
    // Build payload object
    const payload = {
      type: type,
      requestid: request_id,
      data: data,
    };
    // Verify payload
    const errMsg = state_data.Input.verify(payload);
    if (errMsg) throw new Error(errMsg);
    // Create message
    const message = state_data.Input.create(payload);
    // Encode message
    const buffer = state_data.Input.encode(message).finish();
    return buffer;
  };

  // //处理聊天数据表情
  // const initMessage = (text: any) => {
  //   const EMOJI_REMOTE_SRC =
  //     'https://cdn.jsdelivr.net/npm/emoji-datasource-apple@6.0.1/img/apple/64';
  //   // 定义一个函数来将特定格式的字符串替换为图片
  //   function replaceWithEmojiImages(text: any) {
  //     // 定义一个正则表达式来匹配 /:1f600:/ 格式的字符串
  //     const regex = /\/:(.*?):\//g;

  //     // 使用 replace 方法和回调函数进行替换
  //     return text.replace(regex, (match: any) => {
  //       const value = match.slice(2, -2);
  //       // 构建图片标签
  //       const imgSrc = `${EMOJI_REMOTE_SRC}/${value}.png`; // 假设图片存储在这个路径
  //       return `<ImgT data-code="${value}" src="${imgSrc}"  width="20" height="20" class="emoji-img"/>`;
  //     });
  //   }
  //   // 调用函数进行替换
  //   const outputText = replaceWithEmojiImages(text);
  //   return outputText;
  // };

  // 获取快捷语--分类列表
  const getShortcutCatelist = () => {
    const sendReq = state.root.lookupType('QuickPhrasesCListListReq');
    state.requestid++;
    const requestid = state.requestid;
    const type = 23; //
    const payload = {
      deviceid: state.deviceID,
      page: 1,
      pagesize: 200,
    };
    //编码消息体
    const errMsg2 = sendReq.verify(payload);
    if (errMsg2) throw new Error(errMsg2);
    const decodedata = sendReq.encode(
      sendReq.create(payload),
    ).finish();
    const encodedRequest = encodeInput(type, requestid, decodedata);
    IWebsocket.sendMessageHandler(encodedRequest);
  };
  // 接收快捷语--分类列表
  const getShortcutCateMsg = (decodeobj1: any) => {
    if (decodeobj1.data) {
      const decodeobj00 = decodeContent(decodeobj1.data, 'QuickPhrasesCListRsp');
      state.shortCutTyptList = decodeobj00.quickphrasec;
      console.log('快捷语分类列表', state.shortCutTyptList);
    }
  };
  // 获取快捷语列表
  const getShortcutlist = () => {
    const sendReq = state.root.lookupType('QuickPhrasesListReq');
    state.requestid++;
    const requestid = state.requestid;
    const type = 19; //
    const payload = {
      deviceid: state.deviceID,
      page: 1,
      pagesize: 200,
    };
    //编码消息体
    const errMsg2 = sendReq.verify(payload);
    if (errMsg2) throw new Error(errMsg2);
    const decodedata = sendReq.encode(
      sendReq.create(payload),
    ).finish();
    const encodedRequest = encodeInput(type, requestid, decodedata);
    IWebsocket.sendMessageHandler(encodedRequest);
  };
  // 接收快捷语列表
  const getShortcutMsg = (decodeobj1: any) => {
    if (decodeobj1.data) {
      const decodeobj00 = decodeContent(decodeobj1.data, 'QuickPhrasesListRsp');
      state.quickPhrasesList = decodeobj00.quickphrase;
      console.log('快捷语列表', state.quickPhrasesList);
      //   快捷语自动回复状态保存
      let arr = state.quickPhrasesList
      let bol = false
      let text = ''
      arr.map((item: any) => {
        if (item.isautorsp == 1) {
          bol = true
          text = item.content
        }
      })
      chat.setAutoreply(bol)
      chat.setAutoreplyText(text)
    }
  };
  // 快捷语列表编辑
  const setShortcutList = (data: any) => {
    const sendReq = state.root.lookupType('QuickPhrasesModifyReq');
    state.requestid++;
    const requestid = state.requestid;
    const type = data?.mType; // type: 16 新增快捷语, 17 修改， 18 删除
    const payload = {
      id: data?.id,//快捷语id只有删除和修改的时候传，新增的时候不传
      qhcid: data?.qhcid, //分类id
      deviceid: state.deviceID, //用户id
      istop: data?.istop || 2, //1为置顶 其余值不置顶
      sort: data?.sort || 1, //排序，这个需要前端自己定义数字
      isautorsp: data?.isautorsp || 6, //是否是自动回复 前端用的
      content: data?.content || '', // 快捷语的内容
    };
    //编码消息体
    const errMsg2 = sendReq.verify(payload);
    if (errMsg2) throw new Error(errMsg2);
    const decodedata = sendReq.encode(
      sendReq.create(payload),
    ).finish();
    const encodedRequest = encodeInput(type, requestid, decodedata);
    IWebsocket.sendMessageHandler(encodedRequest);
  };
  // 发送添加快捷语--分类请求
  const setShortcutCateList = (data: any) => {
    const sendReq = state.root.lookupType('QuickPhrasesCModifyReq');
    state.requestid++;
    const requestid = state.requestid;
    const type = data?.mType; // type: 20 新增, 21 修改， 22 删除
    const payload = {
      deviceid: state.deviceID, //用户id
      id: data?.id, // id只有删除和修改的时候传，新增的时候不传
      sort: data?.sort || 1, //排序，这个需要前端自己定义数字
      title: data?.title || '', //分类的标题
    };
    //编码消息体
    const errMsg2 = sendReq.verify(payload);
    if (errMsg2) throw new Error(errMsg2);
    const decodedata = sendReq.encode(
      sendReq.create(payload),
    ).finish();
    const encodedRequest = encodeInput(type, requestid, decodedata);
    IWebsocket.sendMessageHandler(encodedRequest);
  };
  return {
    ...toRefs(state_data),
    encodeInput,
    decodeContent,

    getShortcutCatelist,
    getShortcutCateMsg,
    getShortcutlist,
    getShortcutMsg,
    setShortcutList,
    setShortcutCateList,
  };
};
export default usechatHooks;
